package com.climb.moive.controller;

import com.climb.moive.entity.Member;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.MemberService;
import com.climb.moive.service.MovieService;
import com.climb.moive.service.impl.MemberServiceImpl;
import com.climb.moive.service.impl.MovieServiceImpl;
import com.climb.moive.util.CommmonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @author Administrator
 * 根据提交的方法跳转至不同的页面
 */
@WebServlet("/memberIndex")
public class MemberIndexController extends BaseController {

    /*跳转至页面方法*/
    @Override
    public String index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        MovieService movieService=new MovieServiceImpl();
        /*搜索近期热映电影recentlyReleaseMovieList*/
        req.setAttribute("recentlyReleaseMovieList",movieService.findRecentlyReleaseMovies());
        /*分类电影搜索*/
        /*动作类actionMovieList*/
        req.setAttribute("actionMovieList",movieService.findMoviesbyType("动作片"));
        /*喜剧类comedyMovieList*/
        req.setAttribute("comedyMovieList",movieService.findMoviesbyType("喜剧片"));
        /*爱情loveMovieList*/
        req.setAttribute("loveMovieList",movieService.findMoviesbyType("爱情片"));
        /*科幻scienceFictionMovieList*/
        req.setAttribute("scienceFictionMovieList",movieService.findMoviesbyType("科幻片"));
        /*语言分类*/
        /*华语chineseMovieList*/
        req.setAttribute("chineseMovieList",movieService.findMoviesbyLanguage("中文"));
        /*欧美englishMovieList*/
        req.setAttribute("englishMovieList",movieService.findMoviesbyLanguage("英语"));
        return "/view/member/MemberIndex.jsp";
    }

    public String doRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "forward:/view/member/MemberRegister.jsp";
    }

    public String doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "forward:/view/member/MemberLogin.jsp";
    }

    /*处理成员注册请求*/
    public String register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = "";
        try {
            /*获取表信息并封装到对象里*/
            Member member = new Member();
            //通过request.getParameterMap()获取表单对象
            //封装对象
            CommmonUtils.parseBean(member, request.getParameterMap());
            //注册时间由服务端给出
            member.setRegister_time(new Date());
            System.out.println(member);
            MemberService memberService = new MemberServiceImpl();
            memberService.insert(member);
            /*如果插入成功就跳转到登陆页面*/
            url = "redirect:" + request.getContextPath() + "/memberIndex?method=doLogin";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return url;
    }

    //处理登录请求
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = null;
        try {
            //1、获得请求数据
            Member member = new Member();
            CommmonUtils.parseBean(member, request.getParameterMap());

            //2、调用业务处理
            MemberService memberService = new MemberServiceImpl();
            Member loginMember = memberService.login(member);

            //3、返回结果。成功重写向到首页，失败重新登录(forward)
            if (loginMember != null) {
                HttpSession session = request.getSession();
                session.setAttribute("member", loginMember);

                url = "redirect:" + request.getContextPath() + "/memberIndex";
            } else {
                // 失败重新登录(forward)
                request.setAttribute("message", "用户名/密码出错");
                url = "/view/member/MemberLogin.jsp";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return url;
    }

    /**注销*/
    public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();

        //在首页中有登录的链接
        return "redirect:" + request.getContextPath() + "/memberIndex";
    }

    /**模糊搜索*/
    public String search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*获取名字*/
        String name =request.getParameter("movieName");
        MovieService movieService=new MovieServiceImpl();
        request.setAttribute("movieList",movieService.findMoveByMovieName(name));
        //在首页中有登录的链接
        return "forward:/view/member/MemberSearchMovieList.jsp";
    }


}
